<?php

class MY_Model extends CI_Model {

    protected $_data;

    function __construct() {
        parent::__construct();
    }

    protected $fields = null;
    protected $table = null;
    protected $join = null;

    protected function table() {
        return FALSE;
    }

    protected function validation() {
        return false;
    }

    protected function attribute() {
        return false;
    }

    protected function join() {
        return false;
    }

    protected function get_table() {
        if (!$this->table()) {
            show_error("undefinded function table()");
        } else {
            return $this->table();
        }
    }

//    public function __set($name, $value) {
//        $this->_data[$name] = $value;
//    }
//    
//    public function __get($name) {
//        if(isset ($this->_data[$name])){
//            return $this->data[$name];
//        }
//    }

    private function get_primary_key() {
        if ($this->attribute()) {
            $val = $this->attribute();
            if (isset($val["primary"]))
                return $val["primary"];
            else
                show_error("undefinded primarykey");
        }else
            show_error("undefinded primarykey");
    }

    //Khoi tao truoc khi select
    private function pri_select() {
        $join = $this->join();
        $this->db->from($this->get_table());
        if ($this->fields)
            $this->db->select($this->fields);
        if ($join && count($join) > 0) {
            foreach ($join as $key => $val) {
                $this->db->join($key, $val);
            }
        }
    }

    private function pri_valid() {
        $this->load->library("validation");
        if ($this->validation->run()) {
            return true;
        } else {
            return false;
        }
    }

    public function find_all($fields = "",$limit=null,$offset=null,$orderattr=null,$order=null) {
        $this->pri_select();
		if($order !== null && $orderattr!=null){
			$this->db->order_by($orderattr,$order);
		}
        if($limit !== null && $offset !== null)
            $this->db->limit($limit, $offset);
        else if($limit !== null)
            $this->db->limit($limit);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    public function find_by_primary($key) {
        $this->pri_select();
        $query = $this->db->where($this->get_primary_key(), $key)->get();
        return $query->row();		
    }

	public function get_count($attr=null, $value=null){
		if(!empty($attr)){
			$this->db->where($attr, $value);	
		}		
		$query = $this->db->get($this->get_table());
		return $query->num_rows();
	}
	
    public function find_all_by_attr($attr, $value,$limit = null,$offset=null,$orderattr=null,$order=null) {
        $this->pri_select();
		if($order !== null && $orderattr!=null){
			$this->db->order_by($orderattr,$order);
		}
        if($limit !== null && $offset !== null)
            $this->db->limit($limit, $offset);
        else if($limit !== null)
            $this->db->limit($limit);
        $query = $this->db->where($attr, $value)->get();
        return $query->result();
    }

    public function insert($data = null) {
        if ($this->db->insert($this->table(), $data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function update($key, $data) {
        $this->db->where($this->get_primary_key(), $key);
        if ($this->db->update($this->get_table(), $data)) {
            return true;
        } else {
            return false;
        }
    }

    public function delete($key) {
        $this->db->where($this->get_primary_key(), $key);
        if ($this->db->delete($this->get_table())) {
            return true;
        } else {
            return false;
        }
    }

}

?>